package com.example.mycarcalling.activity;


import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.lib_cache.model.User;
import com.example.lib_cache.service.ApiService;
import com.example.lib_cache.viewmodel.LoginActivityViewModel;
import com.example.lib_common.util.SpUtils;
import com.example.mycarcalling.R;
import com.example.mycarcalling.databinding.ActivityLoginBinding;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class LoginActivity extends AppCompatActivity {

    @Inject
    ApiService apiService;

    private final static int LOGIN_COMPETE = 1;
    private boolean pwdSwitch = false;
    private Context context = null;

    private ActivityLoginBinding activityLoginBinding;
    private LoginActivityViewModel loginActivityViewModel;
    public final static String TAG = LoginActivity.class.getSimpleName();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         activityLoginBinding = DataBindingUtil.setContentView(
                this,R.layout.activity_login);
        context = this;
         setTitle("登录");

        //设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(100);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(300, 300);

        Glide.with(this)
                .load(R.mipmap.pexels)
                .apply(options)
                .into(activityLoginBinding.ivLogin);

//        setContentView(R.layout.activity_login);

        //创建数据源
        //创建一个ViewModel的工厂
        ViewModelProvider.Factory factory = ViewModelProvider.
                AndroidViewModelFactory.getInstance(getApplication());
        //创建ViewModel的管理对象
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        //生产ViewModel
         loginActivityViewModel = viewModelProvider.
                get(LoginActivityViewModel.class);
//        loginActivityViewModel =  DefaultViewModelFactories.getActivityFactory(this)
//                .create(LoginActivityViewModel.class);

        activityLoginBinding.setLifecycleOwner(this);

        String rememberAcc = SpUtils.getString(this,"rememberAcc");
        String rememberPwd = SpUtils.getString(this,"rememberPwd");
        Boolean rememberCheck = SpUtils.getBoolean(this,"rememberCheck",false);


        if(rememberCheck){
            loginActivityViewModel.setUser(new User(rememberAcc,rememberPwd));
            activityLoginBinding.setMainViewModel(loginActivityViewModel);
            activityLoginBinding.cbRememberPwd.setChecked(rememberCheck);
        }

        activityLoginBinding.ivPwdSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pwdSwitch = !pwdSwitch;
                if (pwdSwitch){
                    activityLoginBinding.ivPwdSwitch.setImageResource(
                            R.drawable.ic_visibility_black_24dp);
                    activityLoginBinding.etPwd.setInputType(
                            InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else {

                    activityLoginBinding.ivPwdSwitch.setImageResource(
                            R.drawable.ic_baseline_visibility_off_24);

                    activityLoginBinding.etPwd.setInputType(
                            InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                    activityLoginBinding.etPwd.setTypeface(Typeface.DEFAULT);
                }
            }
        });

        activityLoginBinding.btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "开始登录");
                loginActivityViewModel.setUser(
                        new User(
                                activityLoginBinding.etAccount.getText().toString().trim(),
                                activityLoginBinding.etPwd.getText().toString().trim()));
                activityLoginBinding.setMainViewModel(loginActivityViewModel);

                SpUtils.putString(v.getContext(),"rememberAcc",
                        activityLoginBinding.etAccount.getText().toString().trim());
                SpUtils.putString(v.getContext(),"rememberPwd",
                        activityLoginBinding.etPwd.getText().toString().trim());
                SpUtils.putBoolean(v.getContext(),"rememberCheck",
                        activityLoginBinding.cbRememberPwd.isChecked());
                loginActivityViewModel.loginRequest(apiService,v.getContext(),
                        activityLoginBinding.etAccount.getText().toString().trim(),
                        activityLoginBinding.etPwd.getText().toString().trim());

                if (!SpUtils.getString(v.getContext(),"cctoken").equals("")) {
                    Log.d(TAG,"验证登录成功");

                    Toast.makeText(v.getContext(),"登录成功",Toast.LENGTH_SHORT).show();
                    setResult(LOGIN_COMPETE);
                    finish();
                }

            }
        });

        loginActivityViewModel.getResult().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    Log.d(TAG,"验证登录成功");
                    Toast.makeText(context,"登录成功",Toast.LENGTH_SHORT).show();
                    setResult(LOGIN_COMPETE);
                    finish();
                }
            }
        });

        activityLoginBinding.tvSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(),RegisterActivity.class);
                startActivity(intent);
            }
        });
    }
}
